<?php

namespace app\admin\controller;

use app\common\controller\Base;
use think\Request;

class Role extends Base
{
    /**
     * 显示资源列表
     */
    public function index()
    {
        $count = db('role')->count();
        $data = db('role')->select();
        $info = db('rule')->select();
        $list = get_trees($info);
        $this->assign('data', $data);
        $this->assign('count', $count);
        $this->assign('list', $list);
        return view();
    }


    /**
     * 显示添加资源表单页.
     */
    public function create()
    {
        return view();
    }

    /**
     * 显示编辑资源表单页.
     */
    public function edit()
    {
        $id = input('get.id/d',0);
        $data = db('role')->field('title,description,status')->where('id',$id)->find();
        $this->assign('id', $id);
        $this->assign('data', $data);
        return view();
    }

    /**
     * 保存更新新建的资源
     */
    public function save()
    {
        if(request()->isPost()){
            $data['id'] = input('post.id/d','');
            $data['title'] = input('post.title/s','','/([\x{4e00}-\x{9fa5}]+)/u');
            $data['title'] || jump_page('','角色名称必须是汉字!',2);
            $info = db('role')->where('id','NOT IN',$data['id'])->where('title', $data['title'])->find();
            $info && jump_page('','该规则名称已存在!',2);
            $data['description'] = input('post.description/s','','/([\x{4e00}-\x{9fa5}]+)/u');
            $data['description'] || jump_page('','描述必须是汉字!',2);
            $data['status'] = input('post.status/d',1);
            if($data['id']){
                (db('role')->where('id', $data['id'])->update($data)) !== false ? _layer_msg('已更新!','',1) : jump_page('','更新失败!',2);
            }else{
                db('role')->where('title',$data['title'])->find() &&  _layer_msg('该角色已存在!','admin/role/create');
                db('role')->insert($data) ? _layer_msg('已添加!','',1) : _layer_msg('添加失败!','admin/role/create');
            }
        }
    }

    /**
     * 状态操作
     */
    public function status()
    {
        $id = input('post.id', '');
        $status = input('post.status', '');
        return ($status ? db('role')->where('id', $id)->update(['status' => '0']) : db('role')->where('id', $id)->update(['status' => '1'])) ? json(['status'=>1]) : json(['status'=>0,'msg'=>'更新状态失败!']);
    }

    /**
     * 删除指定资源
     */
    public function delete()
    {
        $ids = input('post.id','');
        if(!$ids) return json(['status'=>0,'msg'=>'请选择要删除的角色!']);
       return db('role')->delete($ids) ? json(['status'=>1,'msg'=>'已删除!']) : json(['status'=>0,'msg'=>'删除失败!']);
    }

    /**
     * 设置权限
     */
    public function setRule(){
        $data = input('post.');
        if(!$data['id']) return json(['status'=>0,'msg'=>'请选择要添加权限的角色']);
        $data['rules'] = implode(',',array_pop($data));
//        print_r($data);exit;
        return (db('role')->update($data)) !== false ? json(['status'=>1,'msg'=>'请添加权限成功!']) : json(['status'=>0,'msg'=>'添加权限失败!']);

    }
}
